/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtOrder;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.entity.ZtQuanself;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.service.ZtColorService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtOrderService;
import com.jeesite.modules.goods.service.ZtQuanselfService;
import com.jeesite.modules.goods.service.ZtSpecificationsService;
import com.jeesite.modules.sys.service.DictDataService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztOrder")
public class ZtOrderController extends BaseController {

	@Autowired
	private ZtOrderService ztOrderService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private ZtColorService ztColorService;
	@Autowired
	private ZtSpecificationsService specificationsService;

	@Autowired
	private ZtGoodsService ztGoodsService;

	@Autowired
	private ZtQuanselfService ztQuanselfService;

	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtOrder get(String id, boolean isNewRecord) {
		ZtOrder ztOrder = new ZtOrder();
		if (StringUtils.isNotBlank(id)) {
			ztOrder.setId(id);
			ztOrder = ztOrderService.get(ztOrder);
		}
		if (ztOrder == null) {
			ztOrder = new ZtOrder();
		}
		return ztOrder;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztOrder:ztOrderData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtOrder ztOrder, Model model) {
		model.addAttribute("ztOrder", ztOrder);

		return "modules/ztOrder/ztOrderList";
	}

	/**
	 * 个人查询列表
	 */

	@RequestMapping(value = "listself")
	public String listself(ZtOrder ztOrder, Model model,HttpSession session) {
		
		if(ztOrder!=null && ztOrder.getZtAdmin()!=null && StringUtils.isNotBlank(ztOrder.getZtAdmin().getId())){
			session.setAttribute("ztOrderAdminid", ztOrder.getZtAdmin().getId());
		}else{
			if(ztOrder==null){ztOrder=new ZtOrder();}
			ZtAdmin ztAdmin=new ZtAdmin();
			ztAdmin.setId(session.getAttribute("ztOrderAdminid").toString());
			ztOrder.setZtAdmin(ztAdmin);
		}
		model.addAttribute("ztOrder", ztOrder);

		return "modules/ztOrder/ztOrderselfList";
	}
	
	/**
	 * 查询列表数据
	 */

	@RequestMapping(value = "listselfData")
	@ResponseBody
	public Page<ZtOrder> listselfData(ZtOrder ztOrder, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ztOrder.setOrderstatus("0");
		
		Page<ZtOrder> page = ztOrderService.findPage(new Page<ZtOrder>(request,
				response), ztOrder);

		return page;
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztOrder:ztOrderData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtOrder> listData(ZtOrder ztOrder, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ztOrder.setOrderstatus("0");
		
		Page<ZtOrder> page = ztOrderService.findPage(new Page<ZtOrder>(request,
				response), ztOrder);

		

		return page;
	}

	DictDataService dictDataService;

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztOrder:ztOrderData:view")
	@RequestMapping(value = "form")
	public String form(@ModelAttribute("ztOrder") ZtOrder ztOrder, Model model,
			HttpServletRequest request, HttpServletResponse response) {

		if (ztOrder.getCreateDate() == null) {
			ztOrder.setCreateDate(new Date());
		}
		if (!ztOrder.getIsNewRecord()) {
			
			
			if(StringUtils.isBlank(ztOrder.getOrderstatus())){
				ztOrder.setOrderstatus("0");
			}
			if(Integer.valueOf(ztOrder.getOrderstatus())>1){
				ztOrder.setOrderstatus("1");
			}
			
			ztOrder.setOrderstatus(DictUtils.getDictLabel(
					"zt_orderstatus_type", ztOrder.getOrderstatus(), "待付款"));
			String goods = ztOrder.getGoods();

			List<ZtGoods> ztGoodsList = new ArrayList<ZtGoods>();
			List<ZtQuan> ztQuanselfList = new ArrayList<ZtQuan>();

			if (StringUtils.isNotBlank(goods)) {
				
				JSONArray goodsArr = JSONArray.parseArray(goods);
				
				for (int i = 0; i < goodsArr.size(); i++) {
					JSONObject jno=goodsArr.getJSONObject(i);
					

					ZtGoods ztGoods = ztGoodsService.get(jno.getString("goodsid"));

					//	"goods":[{"goodsid":"1108185384080601088","numCount":"2","colorid":"1108292291517095936","specificationsid":"1108292291626147840",
			       //  "freight":"0","price":"10","trolleyid":""}],
					
					ZtColor ztColor=new ZtColor();
					ztColor.setId(jno.getString("colorid"));
					ztColor=ztColorService.get(ztColor);
					ztGoods.setColor(ztColor.getTitle());
					String specificationsid="";
					if(jno.get("specificationsid")!=null){
						specificationsid=jno.get("specificationsid").toString();
						
					}
					if(StringUtils.isNotBlank(specificationsid)){
						ZtSpecifications ztSpecifications = new ZtSpecifications();
						ztSpecifications.setId(specificationsid);
						ztSpecifications=specificationsService.get(ztSpecifications);
						if(ztSpecifications!=null){
							specificationsid=ztSpecifications.getName();
							
						}
					}
					ztGoods.setCode(specificationsid);
					ztGoods.setCount(Integer.valueOf(jno.get("numCount").toString()));
					
					double price=Double.valueOf(jno.getString("price"));
					ztGoods.setPrice(price);
					ztGoods.setOrigPrice(Double.valueOf(jno.getString("goodsPrice")));
				//	ztGoods.setFreight(Double.valueOf(jno.getString("freight")));
					
					
					ztGoodsList.add(ztGoods);
				}

			}
			ztOrder.setZtGoodsList(ztGoodsList);

			ztOrder.setZtQuanselfList(ztQuanselfList);

		}
		
		if(StringUtils.isNotBlank(ztOrder.getQuanselfid())&&!"0".equals(ztOrder.getQuanselfid())){
			/*ZtQuanself ztQuanself=new ZtQuanself();
			ztQuanself.setId(ztOrder.getQuanselfid());
			 ztQuanself = ztQuanselfService.get(ztQuanself);
			ZtQuan ztQuan=ztQuanself.getZtQuan();
			ztOrder.setQuanselfid(String.valueOf(ztQuan.getPrice()));*/
			
			ztOrder.setQuanselfid(String.valueOf(ztOrder.getTotalPrice()+ztOrder.getOrigPrice()));
			
		}else{
			ztOrder.setQuanselfid(String.valueOf(ztOrder.getTotalPrice()+ztOrder.getOrigPrice()));
		}
		
		
		model.addAttribute("ztOrder", ztOrder);
		

		return "modules/ztOrder/ztOrderForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztOrder:ztOrderData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtOrder ztOrder) {

		ztOrderService.save(ztOrder);

		return renderResult(Global.TRUE, text("保存订单成功！"));
	}

	/*
	 * @RequiresPermissions("ztOrder:ztOrderData:edit")
	 * 
	 * @RequestMapping(value = "enable")
	 * 
	 * @ResponseBody public String enable(ZtOrder ztOrder) {
	 * ztOrder.setUsestatus("0"); ztOrderService.updateUsestatus(ztOrder);
	 * return renderResult(Global.TRUE, text("券变更为有效成功")); }
	 */

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztOrder:ztOrderData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtOrder ztOrder) {
		ztOrderService.delete(ztOrder);
		return renderResult(Global.TRUE, text("删除订单成功！"));
	}
}